package com.cloudptt.wp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.ui.HomeActivity;

public class AppCloseReceiver extends BroadcastReceiver {
    private Context ctx = null;
    public AppCloseReceiver(Context ctx)
    {
        this.ctx = ctx;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {
            // 应用关闭逻辑
            doExitAction();
        }
    }

    private void doExitAction(){
        Log.debug(this.getClass(), "doExitAction");
        SysApplication app = ((SysApplication) ctx.getApplicationContext());
        Product product = app.getProduct();
        product.doExit();

        SharedPreferences sharedPreferences = ctx.getSharedPreferences(HomeActivity.KEY_HOME, Context.MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(HomeActivity.KEY_NAME_ISFIRST, false).commit();

        ScreenManager.getScreenManager().popAllActivity();
        SystemClock.sleep(2000);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);

    }
}
